use "$raw/s4_to_s6_noPII.dta", clear
local s4  std_AGG_mock std_MTC_mock std_ENG_mock  AGG_mock  std_MTC std_ENG std_total_score2 std_core_score2
foreach var of local s4 {
ren `var' s4_`var'
}

**************************************
*	Table 6
**************************************

sum stay_school if treatment==0
local mean=r(mean)
reg stay_school treatment school* 
outreg2 using "${output}/table6.tex", dec(2) title(Impact of treatment assignment on continuing from S4 to S6) keep(stay_school treatment  )  addstat(control mean, `mean')  replace
reg stay_school treatment $controls school* s4_std_AGG_mock
outreg2 using "${output}/table6.tex", dec(2) title(Impact of treatment assignment on continuing from S4 to S6) keep(stay_school treatment $controls s4_std_AGG_mock  )  addstat(control mean, `mean')

_pctile s4_AGG_mock if s4_AGG_mock!=.
gen below_median_mock=0 if s4_AGG_mock!=.
replace below_median_mock=1 if s4_AGG_mock<r(r1)
la var below_median_mock "Below median mock"
local subgroup female below_median_mock
foreach x of local subgroup { 
capture gen treatment_`x'=treatment*`x'
local label: var la `x'
la var treatment_`x' "Treatment * `label'"
sum stay_school if `x'==1 & treatment==0
local mean=r(mean)
reg stay_school treatment school* treatment_`x' `x'  $controls s4_std_AGG_mock
test `x' + treatment_`x' + treatment = `x'
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table6.tex", dec(2) adec(2) title(Impact of treatment assignment on continuing from S4 to S6) keep(stay_school treatment treatment_`x' `x' ${controls} s4_std_AGG_mock )  addstat(mean, `mean', treated, `F1c',SE , `F1se', p_value, `p_value') 
}



**************************************
*	variable creation
**************************************
keep if stay_school==1

local principal HIST	ECON	GEOG	ENT IRE	AGRIC	LUG	CRE	PHY	MTC	BIO	CHEM	ART	LIT  KISW  TD  
foreach x of local principal {
replace `x'="6" if `x'=="A"
replace `x'="5" if `x'=="B"
replace `x'="4" if `x'=="C"
replace `x'="3" if `x'=="D"
replace `x'="2" if `x'=="E"
replace `x'="2" if `x'=="E "
replace `x'="1" if `x'=="O"
replace `x'="0" if `x'=="F"
destring `x', replace force
gen `x'_pass=0
replace `x'_pass=1 if `x'>1 & `x'!=.
la var `x'_pass "passed `x'"
}


replace SMA="5" if SMA=="S"
local subsidiary SMA CST GEP
foreach x of local subsidiary {
replace `x'="5" if `x'=="S"
destring `x', replace
replace `x'=18 if `x'==1
replace `x'=17 if `x'==2
replace `x'=16 if `x'==3
replace `x'=15 if `x'==4
replace `x'=14 if `x'==5
replace `x'=13 if `x'==6
replace `x'=12 if `x'==7
replace `x'=11 if `x'==8
replace `x'=10 if `x'==9
replace `x'=`x'-10
}


gen sub_point=0 if GEP!=.
replace sub_point=1 if SMA>=3 & SMA!=.
replace sub_point=sub_point+1 if CST>=3 & CST!=.
replace sub_point=sub_point+1 if GEP>=3 & GEP!=.
label variable sub_point "0-2 if passed subsidiary exams"

egen subsidiary=rowtotal(SMA CST), missing
la var subsidiary "SMA or CST score"
gen subsidiary_gep=subsidiary+GEP
la var subsidiary_gep "subsidary and general paper score"

egen principal=rowtotal(`principal')
la var principal "Principal paper score"

egen passes=rowtotal(*_pass)
la var passes "Number of passes in principal papers"
gen uni_pass=0
replace uni_pass=1 if passes>=2
la var uni_pass "Achieved 2 principal passes"

foreach x of local principal {
egen mean_`x' = mean(`x') if treatment==0
egen sd_`x' = sd(`x') if treatment==0
egen mean_`x'2=max(mean_`x')
la var mean_`x'2 "Control mean of `x'"
drop mean_`x'
egen sd_`x'2=max(sd_`x')
la var sd_`x'2 "Control sd of `x' "
drop sd_`x'
gen std_`x' = (`x' - mean_`x'2) / sd_`x'2
la var std_`x'  "Standardised score in `x'"
}

egen std_principal=rowtotal(std_*), missing
replace std_principal=std_principal/3
la var std_principal "Average of standardised principal scores"

egen mean_sub_point=mean(sub_point) if treatment==0
egen sd_sub_point=sd(sub_point) if treatment==0
egen mean_sub_point2=max(mean_sub_point)
la var mean_sub_point2 "Control mean of sub point"
drop mean_sub_point
egen sd_sub_point2=max(sd_sub_point)
la var sd_sub_point2 "Control sd of sub point"
drop sd_sub_point
gen std_sub_point = (sub_point - mean_sub_point2) / sd_sub_point2
la var std_sub_point "Standarised subsidiary point (0-2)"

egen std_POINTS=rowtotal(std_*), missing
replace std_POINTS=std_POINTS/5
la var std_POINTS "Average of standardised principal points and subsidiary points"


foreach x of local subsidiary {
egen mean_`x' = mean(`x') if treatment==0
egen sd_`x' = sd(`x') if treatment==0
egen mean_`x'2=max(mean_`x')
la var mean_`x'2 "Control mean of `x'"
drop mean_`x'
egen sd_`x'2=max(sd_`x')
la var sd_`x'2 "Control sd of `x'"
drop sd_`x'
gen std_`x' = (`x' - mean_`x'2) / sd_`x'2 
la var std_`x' "Standardised score in`x'"
}


egen std_subsidiary=rowtotal(std_SMA std_CST std_GEP), missing
replace std_subsidiary=std_subsidiary/2
la var std_subsidiary "Average of standardised subsidiary score"
destring POINTS, replace


//renormalise the aggregates
foreach x of varlist std_principal std_subsidiary POINTS std_POINTS {
egen mean_`x' = mean(`x') if treatment==0
egen sd_`x' = sd(`x') if treatment==0
egen mean_`x'2=max(mean_`x')
la var mean_`x'2 "Control mean of `x'"
drop mean_`x'
egen sd_`x'2=max(sd_`x')
la var sd_`x'2 "Control sd of `x'"
drop sd_`x'
gen `x'2 = (`x' - mean_`x'2) / sd_`x'2
local label:var la `x'
local label="Re-normalised " + lower("`label'")
la var `x'2 "`label'"
}

sum POINTS2 if treatment==0

local subjects TD ARB KISW ENT LIT ART CHEM BIO MTC PHY CRE LUG AGRIC IRE GEOG ECON HIST CST SMA
foreach x of local subjects {
gen `x'_dummy=0
replace `x'_dummy=1 if `x'!=.
la var `x'_dummy "takes `x' as a principal paper"
tab `x'_dummy
}
replace MTC_dummy=1 if regexm(SUBCOMBINATION, "MATH")==1
tab MTC_dummy female 

**************************************
*	Table A16
**************************************
reg MTC_dummy treatment school* $controls
sum MTC_dummy if treatment==0
local mean=r(mean)
outreg2 using "${output}/table_A16.tex", dec(3) title(Impact of treatment assignment on choose maths as principal subject) keep(MTC_dummy treatment    female  )  addstat(mean, `mean') replace
reg MTC_dummy treatment treatment_female school* $controls
sum MTC_dummy if treatment==0 & female==1
local mean=r(mean)
lincom treatment+ treatment_female  
local F1c = r(estimate)
local F1se = r(se)
test treatment+ treatment_female =0
local p_value = r(p)
outreg2 using "${output}/table_A16.tex", dec(3) title(Impact of treatment assignment on choose maths as principal subject) keep(MTC_dummy treatment treatment_female   female  )  addstat(mean, `mean', overall , `F1c', se ,`F1se', pvalue, `p_value')


**************************************
*	Table A15
**************************************

reg POINTS2 treatment school* ${controls} s4_std_AGG_mock , vce(robust)
outreg2 using "${output}/table_A15.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_POINTS2 treatment   ${controls}) replace

reg std_principal2 treatment school* ${controls} s4_std_AGG_mock , vce(robust)
outreg2 using "${output}/table_A15.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_POINTS2 treatment   ${controls}) 

reg std_subsidiary2 treatment school* ${controls} s4_std_AGG_mock , vce(robust)
outreg2 using "${output}/table_A15.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_POINTS2 treatment   ${controls}) 

local subgroup female 
local i=1 
foreach x of local subgroup { 
reg POINTS2  treatment school* treatment_`x' `x' ${controls} s4_std_AGG_mock, vce(robust)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value=r(p)
outreg2 using "${output}/table_A15.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_POINTS2 treatment treatment_`x' `x' ${controls}) addstat(treated, `F1c', SE , `F1se', p_value, `p_value') 

reg std_principal2  treatment school* treatment_`x'   `x' ${controls} s4_std_AGG_mock, vce(robust)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value=r(p)
outreg2 using "${output}/table_A15.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_principal2 treatment treatment_`x' `x' ${controls}) addstat(treated, `F1c', SE , `F1se', p_value, `p_value')  

reg std_subsidiary2 treatment school* treatment_`x'  `x' ${controls} s4_std_AGG_mock, vce(robust)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A15.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_subsidiary2 treatment treatment_`x' `x' ${controls}) addstat(treated, `F1c',SE , `F1se', p_value, `p_value') 

}


**************************************
*	Table A17
**************************************

gen UNI=1 if University=="yes"
replace UNI=0 if University=="no"
reg UNI  treatment school*  ${controls} s4_std_AGG_mock, vce(robust)
sum UNI if treatment==0
local mean=r(mean)
outreg2 using "${output}/table_A17.tex", dec(2) title(Impact of treatment assignment on applying for university original s4 class)  keep(UNI treatment treatment ${controls}) label replace addstat(mean, `mean')

local subgroup female below_median_mock
foreach x of local subgroup { 
reg UNI  treatment school* treatment_`x' `x' ${controls} s4_std_AGG_mock, vce(robust)
sum UNI if treatment==0 & `x'==1
local mean=r(mean)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value=r(p)
outreg2 using "${output}/table_A17.tex", dec(2) title(Impact of treatment assignment on applying for university original s4 class)  keep(UNI treatment treatment_`x' `x' ${controls}) addstat(mean, `mean', treated, `F1c', SE , `F1se', p_value, `p_value') label 
}

